/*
 * Utils.java
 *
 * Created on 29 April 2008, 12:02
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package trend;
import java.util.*;
/**
 *
 * @author ssripada
 */
public class Utils {
    
    /** Creates a new instance of Utils */
    public Utils() {
    }
    
    
    static public double sigmaX(Set dateSet){
        double rtn = 0;
        int j = 0;
        Double first = new Double(0);
        for(Iterator i = dateSet.iterator();i.hasNext();){
            Double key = (Double)i.next();
            if(j==0)
                first = key;
            rtn = rtn + (key.doubleValue()-first.doubleValue());
            j++;
        }
        return rtn;
    }

static public double sigmaXSquare(Set dateSet){
    double rtn = 0;
    int j = 0;
    Double first = new Double(0);
    for(Iterator i = dateSet.iterator();i.hasNext();){
        Double key = (Double)i.next();
        if(j==0)
            first = key;
        rtn = rtn + Math.pow((key.doubleValue()-first.doubleValue()),2);
        j++;
    }
    return rtn;
}



static public double sigmaY(Collection values){
    double rtn = 0;
    for(Iterator i = values.iterator();i.hasNext();){
        Double ctVal = (Double)i.next();
        rtn = rtn + ctVal.doubleValue();
    }
    return rtn;
}


static public double sigmaYSquare(Collection values){
    double rtn = 0;
    for(Iterator i = values.iterator();i.hasNext();){
        Double ctVal = (Double)i.next();
        rtn = rtn + Math.pow(ctVal.doubleValue(),2);
    }
    return rtn;
}

static public double sigmaXY(TreeMap series){
    double rtn = 0;
    Double first = (Double)series.firstKey();
    for(Iterator i = series.entrySet().iterator();i.hasNext();){
        Map.Entry e = (Map.Entry) i.next();
        Double key = (Double)e.getKey();
        Double ctVal = (Double)e.getValue();
        rtn = rtn + ((key.doubleValue()-first.doubleValue())*ctVal.doubleValue());
    }
    return rtn;
}


public static double  getLeastSquareSlope(double sigX,double sigX2,double sigY,double sigXY, int N){
        double num = ((N*sigXY) - (sigX*sigY));
        double den = ((N*sigX2) - Math.pow(sigX,2));
        return num/den;
    }

    public static double  getLeastSquareC(double sigX,double sigX2,double sigY,double sigXY,int N){
        double num = ((sigX2*sigY) - (sigXY*sigX));
        double den = ((N*sigX2) - Math.pow(sigX,2));
        return num/den;
    }
}
